using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Models.BUS
{
	#region using
	using TemplateWeb.Helpers;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
    using TemplateWeb.DAO;
    using TemplateWeb.DTO;
    using System.Data;
	#endregion
	public class SanPhamBUS
	{
        public List<SanPhamDTO> SelectAll()
        {
            try
            {
                var lstDto = new List<SanPhamDTO>();
                var rpt = new SanPhamDAO(WebConfiguration.DbConnectionString);
                var objDS = rpt.SelectAll();
                if (objDS != null && objDS.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in objDS.Tables[0].Rows)
                    {
                        lstDto.Add(SanPhamDTO.Translate(row));
                    }
                }
                return lstDto;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("SanPham - SelectAll: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("SanPham - SelectAll: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return new List<SanPhamDTO>();
            }
        }

        public SanPhamDTO SelectByID(int id)
        {
            try
            {
                var rpt = new SanPhamDAO(WebConfiguration.DbConnectionString);
                var objDS = rpt.SelectByID(id);
                if (objDS != null && objDS.Tables[0].Rows.Count > 0)
                {
                    return SanPhamDTO.Translate(objDS.Tables[0].Rows[0]);
                }
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("SanPham - SelectByID: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("SanPham - SelectByID: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
            }
            return null;
        }

        public bool Insert(SanPhamDTO obj)
        {
            try
            {
                var rpt = new SanPhamDAO(WebConfiguration.DbConnectionString);
                return rpt.Insert(obj);
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("SanPham - Insert: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("SanPham - Insert: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return false;
            }
        }

        public bool Delete(int id)
        {
            try
            {
                var rpt = new SanPhamDAO(WebConfiguration.DbConnectionString);
                return rpt.Delete(id);
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("SanPham - Delete: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("SanPham - Delete: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return false;
            }
        }

        public bool Update(SanPhamDTO obj)
        {
            try
            {
                var rpt = new SanPhamDAO(WebConfiguration.DbConnectionString);
                return rpt.Update(obj);
            }
            catch
            {
                return false;
            }
        }

		public List<SanPhamDTO> SelectByIDDanhMucCon(int iddanhmuccon)
		{
			try
			{
                var lstDto = new List<SanPhamDTO>();
                var rpt = new SanPhamDAO(WebConfiguration.DbConnectionString);
                var objDS = rpt.SelectByIDDanhMucCon(iddanhmuccon);
                if (objDS != null && objDS.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in objDS.Tables[0].Rows)
                    {
                        lstDto.Add(SanPhamDTO.Translate(row));
                    }
                }
                return lstDto;
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					CommonLogging.Logger.Error("SanPham - SelectByIDDanhMucCon: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					CommonLogging.Logger.Error("SanPham - SelectByIDDanhMucCon: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				return new List<SanPhamDTO>();
			}
		}

        public List<SanPhamDTO> SelectByIDToChucCaNhan(int idtochuccanhan)
		{
            try
            {
                var lstDto = new List<SanPhamDTO>();
                var rpt = new SanPhamDAO(WebConfiguration.DbConnectionString);
                var objDS = rpt.SelectByIDToChucCaNhan(idtochuccanhan);
                if (objDS != null && objDS.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in objDS.Tables[0].Rows)
                    {
                        lstDto.Add(SanPhamDTO.Translate(row));
                    }
                }
                return lstDto;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("SanPham - SelectByIDToChucCaNhan: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("SanPham - SelectByIDToChucCaNhan: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                return new List<SanPhamDTO>();
            }
		}

        public List<SanPhamDTO> RetrieveByID(int id)
        {
            try
            {
                var lstDto = new List<SanPhamDTO>();
                var rpt = new SanPhamDAO(WebConfiguration.DbConnectionString);
                var objDS = rpt.SelectByID(id);
                if (objDS != null && objDS.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow row in objDS.Tables[0].Rows)
                    {
                        lstDto.Add(SanPhamDTO.Translate(row));
                    }
                }
                return lstDto;
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                    CommonLogging.Logger.Error("SanPham - RetrieveByID: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
                else
                    CommonLogging.Logger.Error("SanPham - RetrieveByID: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
            }
            return new List<SanPhamDTO>();
        }
	}
}